package controlStructure;

import java.util.Scanner;

/**
 * 跳转控制语句(break)
 */
public class Break {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            if (i == 3) {
                break;
            }
            System.out.println("i=" + i);
        }

        //1~100以内的数求和，求出当和第一次大于20的当前数
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            if (sum > 20) {
                System.out.println("sum=" + sum);
                break;
            } else {
                sum += i;
            }
        }

        // 实现登录验证，有3次机会，如果用户名为“丁真”，密码“666”则提示登录成功，否则提示还有几次机会
        int chance = 3;
        Scanner scanner = new Scanner(System.in);
        for (int i = 1; i <= 3; i++) {
            System.out.println("请输入用户名");
            String userName = scanner.next();
            System.out.println("请输入密码");
            String password = scanner.next();
            if("丁真".equals(userName) && "666".equals(password)) {
                System.out.println("登录成功！");
                break;
            } else {
                chance--;
                System.out.println("登录失败，还有" + chance + "次机会");
            }
        }

    }
}
